/****************************************************************************
**
** TextData.cpp
**
** Copyright (C) August 2014 Hotride
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
*****************************************************************************
*/
//---------------------------------------------------------------------------
#include "stdafx.h"
//---------------------------------------------------------------------------
//--------------------------------TTextData---------------------------------
//---------------------------------------------------------------------------
TTextData::TTextData()
: m_Unicode(false), m_Type(TT_CLIENT), m_Font(0), m_Color(0), m_Timer(0),
m_Next(NULL), m_Prev(NULL)
{
	m_Text = "";
	m_UnicodeText = L"";

	m_Texture.Width = 0;
	m_Texture.Height = 0;
}
//---------------------------------------------------------------------------
TTextData::TTextData(TTextData *obj)
: m_Unicode(obj->GetUnicode()), m_Type(obj->GetType()), m_Font(obj->GetFont()),
m_Color(obj->GetColor()), m_Timer(obj->GetTimer()),
m_Next(NULL), m_Prev(NULL)
{
	m_Text = obj->GetText();
	m_UnicodeText = obj->GetUnicodeText();

	m_Texture.Width = 0;
	m_Texture.Height = 0;
}
//---------------------------------------------------------------------------
TTextData::~TTextData()
{
	ClearTexture();
}
//---------------------------------------------------------------------------
void TTextData::GenerateTexture(float MaxWidth, WORD flags, TEXT_ALIGN_TYPE align, BYTE cell, int font)
{
	if (m_Unicode)
	{
		if (font == -1) font = m_Font;

		UO->m_Font->GenerateW(font, m_Texture, m_UnicodeText.c_str(), m_Color, cell, MaxWidth, align, flags);
	}
	else UO->m_Font->GenerateA(m_Font, m_Texture, m_Text.c_str(), m_Color, MaxWidth, align, flags);

	if (!m_Texture.Empty())
	{
		m_Timer += (m_Texture.Lines * (UO->m_ConfigManager.GetSpeechDelay() * 20));
	}
}
//---------------------------------------------------------------------------
//-----------------------------TTextContainer-------------------------------
//---------------------------------------------------------------------------
TTextContainer::TTextContainer(int max_size)
: m_MaxSize(max_size), m_Size(0), m_Height(0), m_SystemHeight(0),
m_ObjectsHeight(0), m_ClientHeight(0), m_Head(NULL), m_Top(NULL),
m_SelectionIndex(0)
{
}
//---------------------------------------------------------------------------
TTextContainer::~TTextContainer()
{
	Clear();
}
//---------------------------------------------------------------------------
void TTextContainer::Clear()
{
	if (m_Head != NULL)
	{
		TTextData *obj = m_Head;
		while (obj != NULL)
		{
			TTextData *prev = obj->m_Prev;
			delete obj;
			obj = prev;
		}
	}

	m_Top = NULL;
	m_Head = NULL;

	m_Size = 0;
	m_SelectionIndex = 0;

	m_Height = 0;
	m_SystemHeight = 0;
	m_ObjectsHeight = 0;
	m_ClientHeight = 0;
}
//---------------------------------------------------------------------------
void TTextContainer::Add(TTextData *obj)
{
	if (m_Head != NULL)
	{
		m_Head->m_Next = obj;
		obj->m_Prev = m_Head;
		obj->m_Next = NULL;
		m_Head = obj;

		if (m_Size >= m_MaxSize)
		{
			TTextData *top = m_Top->m_Next;

			m_Height -= m_Top->GetTexture().Height;

			if (m_Top->GetType() == TT_SYSTEM) m_SystemHeight -= m_Top->GetTexture().Height;
			else if (m_Top->GetType() == TT_OBJECT) m_ObjectsHeight -= m_Top->GetTexture().Height;
			else if (m_Top->GetType() == TT_CLIENT) m_ClientHeight -= m_Top->GetTexture().Height;

			delete m_Top;

			m_Top = top;
			m_Top->m_Prev = NULL;
		}
		else m_Size++;
		
		if (m_SelectionIndex && m_SelectionIndex < m_Size) m_SelectionIndex++;

		m_Height += obj->GetTexture().Height;

		if (obj->GetType() == TT_SYSTEM) m_SystemHeight += obj->GetTexture().Height;
		else if (obj->GetType() == TT_OBJECT) m_ObjectsHeight += obj->GetTexture().Height;
		else if (obj->GetType() == TT_CLIENT) m_ClientHeight += obj->GetTexture().Height;
	}
	else
	{
		obj->m_Next = NULL;
		obj->m_Prev = NULL;
		m_Top = obj;
		m_Head = obj;

		m_Size = 1;
		if (m_SelectionIndex) m_SelectionIndex = 0;

		m_Height = obj->GetTexture().Height;

		if (obj->GetType() == TT_SYSTEM) m_SystemHeight = obj->GetTexture().Height;
		else if (obj->GetType() == TT_OBJECT) m_ObjectsHeight = obj->GetTexture().Height;
		else if (obj->GetType() == TT_CLIENT) m_ClientHeight = obj->GetTexture().Height;
	}
}
//---------------------------------------------------------------------------